package leetcode.L138;

import leetcode.L19.ListNode;

import java.util.HashMap;
import java.util.Map;

class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}

public class Solution {
    HashMap<Node, Node> nodeIntegerHashMap = new HashMap<>();
    //后续节点可能未创建
    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        if (!nodeIntegerHashMap.containsKey(head)) {
            Node node = new Node(head.val);
            nodeIntegerHashMap.put(head,node);
            //这里是为了保证拿到next和random
            node.next=copyRandomList(head.next);
            node.random=copyRandomList(head.random);
        }
        return nodeIntegerHashMap.get(head);
    }
}